<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8" style="margin-top: 20px;">
      <el-card shadow="hover">
        <div class="user">
          <img :src="getImageUrl()" alt="">
          <div class="user-info">
            <p>admin</p>
            <p>超级管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登陆时间：<span>2024-12-30</span></p>
          <p>上次登陆地点：<span>河南</span></p>
        </div>
      </el-card>

      <el-card shadow="hover" class="user-table">
        <el-table :data="tableData"
                  :empty-text="emptyData">
          <el-table-column v-for="(val,key) in tableLabel"
                           :key="key"
                           :prop="key"
                           :label="val">

          </el-table-column>
        </el-table>
      </el-card>
    </el-col>
    <el-col :span="16" style="margin-top: 20px">
      <div class="order-total-num">
        <el-card shadow="hover" :body-style="{display:'flex',padding:'0'}" v-for="(item,index) in countData"
                 :key="index">
          <component :is="item.icon" class="icons" :style="{background: item.color}"></component>
          <div class="detail">
            <p class="num">￥{{ item.value }}</p>
            <p class="text">{{ item.name }}</p>
          </div>
        </el-card>
      </div>
      <el-card class="top-echart">
        <div ref="echart" style="height: 280px"></div>
      </el-card>

      <div class="graph">
        <el-card>
          <div ref="userEchart" style="height: 240px"></div>
        </el-card>
        <el-card>
          <div ref="videoEchart" style="height: 240px"></div>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import { reqChartData, reqCountData, reqTableData } from '@/api/home.js'
import * as echarts from 'echarts'

const echart = ref()
const userEchart = ref()
const videoEchart = ref()
const observer = ref(null);
const getImageUrl = (user = 'default') => {
  return new URL(`../assets/images/${user}.png`, import.meta.url).href
}

const tableData = ref([])
const emptyData = ref('暂无数据')
const countData = ref([])
const getTableData = async () => {
  const res = await reqTableData()
  tableData.value = res.data.tableData
}
const getCountData = async () => {
  const res = await reqCountData()
  countData.value = res.data.countData
}

const getChartData = async () => {
  const { data: { orderData, userData, videoData } } = await reqChartData()
  xOptions.xAxis.data = orderData.date
  xOptions.series = Object.keys(orderData.data[0]).map(val => {
        return {
          name: val,
          data: orderData.data.map(item => item[val]),
          type: 'line',
        }
      }
  )
  let oneEchart = echarts.init(echart.value)
  oneEchart.setOption(xOptions)

  xOptions.xAxis.data = userData.map(item => item.data)
  xOptions.series = [
    {
      name: '新增用户',
      data: userData.map(item => item.new),
      type: 'bar',
    },
    {
      name: '活跃用户',
      data: userData.map(item => item.active),
      type: 'bar',
    },
  ]
  let twoEchart = echarts.init(userEchart.value)
  twoEchart.setOption(xOptions)

  pieOptions.series = [
    {
      data: videoData,
      type:'pie'
    }
  ]
  const videoEcahert = echarts.init(videoEchart.value)
  videoEcahert.setOption(pieOptions)

  //监听页面变化
  //如果监听的容器尺寸发生改变，会触发回调函数
  observer.value = new ResizeObserver( () =>{
    oneEchart.resize()
    twoEchart.resize()
    videoEcahert.resize()
  })

  if(echart.value){
    observer.value.observe(echart.value)
  }
}

onMounted(() => {
  getTableData()
  getCountData()
  getChartData()
})

const tableLabel = ref({
  name: '课程',
  todayBuy: '今日购买',
  monthBuy: '本月购买',
  totalBuy: '总购买',
})
const xOptions = reactive({
  textStyle: {
    color: '#999'
  },
  legend: {},
  grid: {
    left: '20%',
  },
  tooltip: {
    trigger: 'axis',
  },
  xAxis: {
    type: 'category',
    data: [],
    axisLine: {
      lineStyle: {
        color: '#17b3a3'
      }
    },
    axisLabel: {
      interval: 0,
      textStyle: {
        color: '#333'
      }
    },
  },
  yAxis: [
    {
      type: 'value',
      axisLine: {
        lineStyle: {
          color: '#17b3a3'
        }
      },
    }
  ],
  color: ['#17b3a3', '#f5b63d', '#ffb980', '#b6a2de', '#d87a80'],
  series: []
})
const pieOptions = reactive({
  tooltip: {
    trigger: 'item',
  },
  legend: {},
  color: [
    '#17b3a3',
    '#f5b63d',
    '#cdb980',
    '#b6a2de',
    '#d87a80',
  ]
})
</script>

<style scoped lang="less">
.home {
  height: 100%;
  overflow: hidden;

  .user {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
    padding-bottom: 20px;
    margin-bottom: 20px;

    img {
      width: 150px;
      height: 150px;
      border-radius: 50%;
      margin-right: 40px;
    }

    .user-info {
      p {
        line-height: 40px;

        &:first-child {
          font-size: 30px;
        }

        &:last-child {
          color: #aaa;
        }
      }


    }
  }

  .login-info {
    p {
      line-height: 30px;
      font-size: 14px;
      color: #999;

      span {
        color: #666;
        margin-left: 50px;
      }
    }
  }

  .user-table {
    margin-top: 20px;
  }


  .order-total-num {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    .el-card {
      width: 32%;
      margin-bottom: 20px;

      .icons {
        width: 80px;
        height: 80px;
        font-size: 30px;
        text-align: center;
        line-height: 80px;
        color: #fff;
      }

      .detail {
        margin-left: 15px;
        display: flex;
        flex-direction: column;
        justify-content: center;

        .num {
          font-size: 30px;
          margin-bottom: 15px;
        }

        .text {
          font-size: 15px;
          text-align: center;
          color: #999;
        }
      }
    }
  }
  .graph{
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    gap: 10px;
    .el-card{
      flex: 1;
      height: 260px;
    }
  }
}
</style>
